﻿Imports System.Data.SQLite

Public Class frmMain

    Private myFile As String = ""
    Private myConnection As SQLiteConnection = New SQLiteConnection

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Me.btnImport.Enabled = False

        txtDB.Text = My.Settings.DataSourceFile
        myFile = txtDB.Text

        myConnection.ConnectionString = "Data Source=" & myFile & ";Version=3;"
        myConnection.Open()

        Me.btnImport.Enabled = True

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnImport.Click

        Dim sSourceItem As String = ""
        Dim sFile As String = ""
        Dim sFileNameAccountName As String = ""

        For i = 0 To Me.lbSourceFiles.Items.Count - 1

            sSourceItem = Me.lbSourceFiles.Items(i).ToString()

            Dim myarray As String() = sSourceItem.Split("|")

            sFile = myarray(0)
            sFileNameAccountName = myarray(1).Substring(0, myarray(1).Length - 4)

            mdlImportQIF.fProcessQIFtoMMEX(sFile, sFileNameAccountName, myConnection)


        Next i

        txtDB.Text = ""
        myFile = ""
        btnImport.Enabled = False

        Me.Close()

        '*// Clean up
        myConnection.Close()


    End Sub


    Private Sub btnSelectDB_Click(sender As System.Object, e As System.EventArgs) Handles btnSelectDB.Click

        With OpenFileDialog1
            .Title = "Please Select valid MMEX database File"
            .InitialDirectory = "C:"

            .ShowDialog()
        End With

    End Sub

    'Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    '    txtDB.Text = OpenFileDialog1.FileName.ToString()
    '    myFile = txtDB.Text




    'End Sub




    Private Sub btnSelectSource_Click(sender As System.Object, e As System.EventArgs) Handles btnSelectSource.Click

        With OpenFileDialog2
            .Title = "Please Select all source QIF files"
            .InitialDirectory = "C:"
            .Filter = "All QIF Files (*.QIF)|*.QIF"
            .Multiselect = True

            .ShowDialog()
        End With

    End Sub

    Private Sub OpenFileDialog2_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog2.FileOk

        Dim filepath() As String = OpenFileDialog2.FileNames
        Dim ffile As String
        For Each ffile In filepath
            Me.lbSourceFiles.Items.Add(System.IO.Path.GetDirectoryName(ffile) & "\" & System.IO.Path.GetFileName(ffile) & "|" & System.IO.Path.GetFileName(ffile))
        Next

    End Sub

    Private Sub btnClearSource_Click(sender As System.Object, e As System.EventArgs) Handles btnClearSource.Click

        lbSourceFiles.Items.Clear()

    End Sub

End Class
